#Author http://www.cnblogs.com/Jame-mei
from conf import settings
import configparser
import os


#1.解析host.ini文件
config=configparser.ConfigParser()
hosts_path=os.path.join(settings.DB_DIR,'host.ini')
config.read(hosts_path)






def get_groups_info(groups):
    '''
    2.通过组名解析出主机名
    :return:
    '''
    group_list=groups.split(',')
    host_list=[]
    for group in group_list:
        if group not in config.options('group'):
            return False,'%s 组不存在，请确认后重新输入!'%group
        else:
            hosts=config.get('group',group) #得到一个字符串
            for host in hosts.split(','):
                host_list.append(host)

    return True, host_list






def get_hosts_info(host):
    '''
    3.通过主机别名获取主机的具体详情
    :param host:
    :return:
    '''
    host_dic={}
    sections=config.sections()
    if host not in sections:
        return False,'主机名:%s 不存在，请配置host.ini或重新输入'%host

    else:
        host_dic['ip'] = config.get(host, 'ip')
        host_dic['port'] = config.getint(host, 'port')
        host_dic['username'] = config.get(host, 'username')
        host_dic['password'] = config.get(host, 'password')
        return True,host_dic


